package com.lys.emsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lys.emsystem.entity.Admin;
import com.lys.emsystem.entity.Student;
import com.lys.emsystem.entity.Teacher;
import com.lys.emsystem.service.IAdminService;
import com.lys.emsystem.service.IStudentService;
import com.lys.emsystem.service.ITeachService;
import com.lys.emsystem.service.ITeacherService;
import com.lys.emsystem.utils.ImageUtils;
import com.lys.emsystem.utils.JWTUtils;
import com.lys.emsystem.utils.Result;
import com.lys.emsystem.vo.CommonVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/system")
@CrossOrigin
public class SystemController {

    @Autowired
    private ImageUtils imageUtils;

    @Autowired
    private IStudentService studentService;

    @Autowired
    private ITeacherService teacherService;

    @Autowired
    private IAdminService adminService;


    @PostMapping("/headImgUpload")
    public Result headImgUpload(@RequestParam(value = "file") MultipartFile file) {
        if (ObjectUtils.isEmpty(file)) {
            return Result.buildResult(Result.Status.ERROR, "请选择图片");
        } else {
            String imgUrl = imageUtils.uploadImageQiniu(file);
            return Result.buildResult(Result.Status.OK, "上传成功",imgUrl);
        }
    }
    //    更换头像
    @PostMapping("/updateAvatar")
    public Result updateAvatar(@RequestHeader("token") String token,@RequestBody CommonVo commonVo) {
    //    获取用户
        Integer userId = JWTUtils.getUserId(token);
        Integer userType = JWTUtils.getUserType(token);
        if (userType == 1) {
        //    学生
            Student student = new Student();
            student.setAvatar(commonVo.getAvatarUrl());
            student.setId(userId);
            if (studentService.updateById(student)) {
                return Result.buildResult(Result.Status.OK);
            }
        }
        if (userType == 2) {
            Teacher teacher = new Teacher();
            teacher.setId(userId);
            teacher.setAvatar(commonVo.getAvatarUrl());
            if (teacherService.updateById(teacher)) {
                return Result.buildResult(Result.Status.OK);
            }
        }
        if (userType == 3) {
            Admin admin = new Admin();
            admin.setId(userId);
            admin.setAvatar(commonVo.getAvatarUrl());
            if (adminService.updateById(admin)) {
                return Result.buildResult(Result.Status.OK);
            }
        }
        return Result.buildResult(Result.Status.ERROR,"更新错误！");
    }

    //    修改密码
    @PostMapping("/updatePass")
    public Result updatePass(@RequestHeader("token") String token, @RequestBody CommonVo commonVo) {
    //    获取当前用户
        Integer userId = JWTUtils.getUserId(token);
        Integer userType = JWTUtils.getUserType(token);
        String olePass = DigestUtils.md5DigestAsHex(commonVo.getOldPassword().getBytes());
        switch (userType) {
            case 1:
                QueryWrapper<Student> wrapper = new QueryWrapper<>();
                wrapper.eq("id", userId);
                Student student = studentService.getOne(wrapper);
                if (olePass.equals(student.getPassword())) {
                    student.setPassword(DigestUtils.md5DigestAsHex(commonVo.getNewPassword().getBytes()));
                    if (studentService.updateById(student)) {
                        return Result.buildResult(Result.Status.OK);
                    } else {
                        return Result.buildResult(Result.Status.ERROR);
                    }
                } else {
                    return Result.buildResult(Result.Status.ERROR,"原密码不正确！");
                }
            case 2:
                QueryWrapper<Teacher> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("id", userId);
                Teacher teacher = teacherService.getOne(wrapper1);
                if (olePass.equals(teacher.getPassword())) {
                    teacher.setPassword(DigestUtils.md5DigestAsHex(commonVo.getNewPassword().getBytes()));
                    if (teacherService.updateById(teacher)) {
                        return Result.buildResult(Result.Status.OK);
                    } else {
                        return Result.buildResult(Result.Status.ERROR);
                    }
                } else {
                    return Result.buildResult(Result.Status.ERROR,"原密码不正确！");
                }
            case 3:
                QueryWrapper<Admin> wrapper2 = new QueryWrapper<>();
                wrapper2.eq("id", userId);
                Admin admin = adminService.getOne(wrapper2);
                if (olePass.equals(admin.getPassword())) {
                    admin.setPassword(DigestUtils.md5DigestAsHex(commonVo.getNewPassword().getBytes()));
                    if (adminService.updateById(admin)) {
                        return Result.buildResult(Result.Status.OK);
                    } else {
                        return Result.buildResult(Result.Status.ERROR);
                    }
                } else {
                    return Result.buildResult(Result.Status.ERROR,"原密码不正确！");
                }
        }
        return null;
    }
}
